import QtQuick
import QtQuick.Layouts
import FileSystemModel
import SystemIconProvider

Item {
    id: fileObjectItem
    
    // 属性
    property var itemId: ""  // 文件或文件夹ID
    property bool isDirectory: false  // 是否为文件夹
    property int iconSize: 32  // 主图标尺寸
    property int statusIconSize: 16  // 状态图标尺寸
    property bool centerIcon: true  // 是否居中显示图标
    property var fileSystemModelParam: null  // 文件系统模型（传递的参数）
    property var systemIconProviderParam: null  // 系统图标提供者（传递的参数）
    property var refreshTrigger: null  // 刷新触发器（用于触发状态更新）
    
    // 获取文件系统模型（优先使用传递的，否则从全局上下文获取）
    function getFileSystemModel() {
        // 优先使用传递的参数
        if (fileSystemModelParam) {
            return fileSystemModelParam;
        }
        // 如果参数为 null，尝试从全局上下文获取
        // 注意：这里直接使用 fileSystemModel，因为我们的属性名是 fileSystemModelParam，不会冲突
        try {
            // 在 QML 中，如果当前作用域没有同名属性，会自动查找全局上下文属性
            var globalModel = fileSystemModel;
            return globalModel || null;
        } catch(e) {
            return null;
        }
    }
    
    // 获取系统图标提供者（优先使用传递的，否则从全局上下文获取）
    function getSystemIconProvider() {
        // 优先使用传递的参数
        if (systemIconProviderParam) {
            return systemIconProviderParam;
        }
        // 如果参数为 null，尝试从全局上下文获取
        try {
            var globalProvider = systemIconProvider;
            return globalProvider || null;
        } catch(e) {
            return null;
        }
    }
    
    // 主图标
    Image {
        id: mainIcon
        width: iconSize
        height: iconSize
        anchors.centerIn: centerIcon ? parent : undefined
        anchors.left: centerIcon ? undefined : parent.left
        anchors.top: centerIcon ? undefined : parent.top
        source: {
            var fsModel = getFileSystemModel();
            if (isDirectory) {
                var folderType = fsModel ? fsModel.getFolderType(itemId) : 1;
                if (folderType === 2) {
                    return "qrc:/icons/folder_root.png";
                }
                return "qrc:/icons/folder.png";
            } else {
                // 根据文件类型获取图标
                var iconProvider = getSystemIconProvider();
                var fileType = "";
                var fileName = fsModel ? fsModel.getFileName(itemId) : "";
                
                console.log("FileObjectItem - 文件图标计算:");
                console.log("  itemId:", itemId);
                console.log("  fileName:", fileName);
                console.log("  iconProvider:", iconProvider);
                
                var lastDot = fileName.lastIndexOf(".");
                if (lastDot !== -1) {
                    fileType = fileName.substring(lastDot + 1).toLowerCase();
                }
                console.log("  fileType:", fileType);
                
                var iconSource = iconProvider ? iconProvider.getFileTypeIcon(fileType) : "";
                console.log("  iconSource:", iconSource);
                
                return iconSource;
            }
        }
        fillMode: Image.PreserveAspectFit
        smooth: true
        antialiasing: true
        sourceSize.width: iconSize
        sourceSize.height: iconSize
    }
    
    // 状态图标容器（右下角）
    Item {
        id: statusIconContainer
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.margins: centerIcon ? 1 : 0
        width: statusIconsRow.width
        height: statusIconsRow.height
        visible: statusIconsRow.width > 0 && statusIconsRow.height > 0
        
        // 获取状态值
        property int currentStatus: {
            if (refreshTrigger !== undefined) {
                // 触发刷新
                var _ = refreshTrigger;
            }
            
            var fsModel = getFileSystemModel();
            
            console.log("FileObjectItem - currentStatus 计算:");
            console.log("  itemId:", itemId);
            console.log("  isDirectory:", isDirectory);
            console.log("  fileSystemModelParam:", fileSystemModelParam);
            console.log("  fsModel (实际使用的):", fsModel);
            
            if (!fsModel) {
                console.log("  fileSystemModel 为 null，返回 -1");
                return -1;
            }
            
            if (!itemId || itemId === "") {
                console.log("  itemId 为空，返回 -1");
                return -1;
            }
            
            var status = -1;
            if (isDirectory) {
                status = fsModel.getFolderStatus(itemId);
                console.log("  getFolderStatus 返回:", status);
            } else {
                status = fsModel.getFileStatus(itemId);
                console.log("  getFileStatus 返回:", status);
            }
            
            return status;
        }
        
        // 状态位定义
        readonly property int _PENDING_DOWNLOAD: 0x00000001      // 待下载
        readonly property int _SYNCED: 0x00000000                // 已同步
        readonly property int _PENDING_MODIFY: 0x00000010         // 待提交变更
        readonly property int _PENDING_ADD: 0x00000100            // 待提交新增
        readonly property int _PENDING_DELETE: 0x00001000         // 待提交删除
        readonly property int _MODIFY_CONFLICT: 0x00100010        // 变更冲突
        readonly property int _SERVER_DELETE_CONFLICT: 0x10000010 // 服务器删除冲突
        readonly property int _LOCAL_DELETE_CONFLICT: 0x00101000 // 本地删除冲突
        
        // 按位检查状态
        function hasFlag(status, flag) {
            console.log("status:", status, "flag:", flag);
            console.log("(status & flag) === flag:", (status & flag) === flag);
            return (status & flag) === flag;
        }
        
        // 状态图标行
        Row {
            id: statusIconsRow
            spacing: 2
            
            // 待下载状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._PENDING_DOWNLOAD)
                source: visible ? "qrc:/icons/status_pending_download.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 已同步状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.currentStatus === statusIconContainer._SYNCED
                source: visible ? "qrc:/icons/status_synced.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 待提交变更状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._PENDING_MODIFY) && 
                        !statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._MODIFY_CONFLICT)
                source: visible ? "qrc:/icons/status_local_modified.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 待提交新增状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._PENDING_ADD)
                source: visible ? "qrc:/icons/status_local_added.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 待提交删除状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._PENDING_DELETE) &&
                        !statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._LOCAL_DELETE_CONFLICT)
                source: visible ? "qrc:/icons/status_local_deleted.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 变更冲突状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._MODIFY_CONFLICT)
                source: visible ? "qrc:/icons/status_modified_conflict.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 服务器删除冲突状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._SERVER_DELETE_CONFLICT)
                source: visible ? "qrc:/icons/status_server_delete_conflict.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
            
            // 本地删除冲突状态图标
            Image {
                width: statusIconSize
                height: statusIconSize
                visible: statusIconContainer.hasFlag(statusIconContainer.currentStatus, statusIconContainer._LOCAL_DELETE_CONFLICT)
                source: visible ? "qrc:/icons/status_local_delete_conflict.png" : ""
                fillMode: Image.PreserveAspectFit
                smooth: true
                antialiasing: true
                sourceSize.width: statusIconSize
                sourceSize.height: statusIconSize
            }
        }
    }
}

